/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServicesAccessData;

import DAO.CounterDAO;
import DAO.StatusDAO;
import DAO.TagDAO;
import DAO.UserDAO;
import Testing.apptien;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.*;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import pojo.Tag;
import Search.solrSearch;

/**
 *
 * @author ngochuy
 */
public class AdminImpl implements Admin.Iface{

    private TTransport tr;
    private Cassandra.Client client;
    public AdminImpl()
    {
        try {
            tr = new TFramedTransport(new TSocket(common.Constants.HOST, common.Constants.PORT));
            TProtocol proto = new TBinaryProtocol(tr);
            client = new Cassandra.Client(proto);
            tr.open();
            client.set_keyspace(common.Constants.KEYSPACE);
        } catch (InvalidRequestException ex) {
            Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public boolean insertStatus(String content, String tags, String state) throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        boolean result = sta_DAO.insertStatus(content, tags, state);
        return result;
    }

    @Override
    public boolean deleteStatus(List<String> liststatusID) throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        boolean result = sta_DAO.DeleteStatus(liststatusID);
        return result;
    }

    @Override
    public boolean updateStateStatus(List<String> StatusID, String newstate) throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        boolean result = sta_DAO.updateStateStatus(StatusID, newstate);
        return result;
    }

    @Override
    public String selectStatusNoTag() throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        String result = sta_DAO.SelectNoTag();
        return result;
    }

    @Override
    public boolean removeTagInStatus(String statusID, String nameTag) throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        boolean result = sta_DAO.removeTagInStatus(statusID, nameTag);
        return result;
    }

    @Override
    public boolean insertTag(List<String> listnameTag) throws TException {
        TagDAO tag_DAO = TagDAO.getInstance(client);
        boolean result = tag_DAO.insertTag(listnameTag);
        return result;
    }

    @Override
    public boolean deleteTag(List<String> listnameTag) throws TException {
        TagDAO tag_DAO = TagDAO.getInstance(client);
        boolean result = tag_DAO.deleteTag(listnameTag);
        return result;
    }

    @Override
    public boolean updateStateTag(List<String> listTagName, String newstate) throws TException {
        TagDAO tag_DAO = TagDAO.getInstance(client);
        boolean result = tag_DAO.updateStateTag(listTagName, newstate);
        return result;
    }

    @Override
    public boolean setBannedUser(List<String> listUserID, String state) throws TException {
        UserDAO user_DAO = UserDAO.getInstance(client);
        boolean result = user_DAO.setBannedUser(listUserID, state);
        return result;
    }

    @Override
    public boolean setUserName(String userID, String userName) throws TException {
        UserDAO user_DAO = UserDAO.getInstance(client);
        boolean result = user_DAO.setUserName(userID, userName);
        return result;
    }

    @Override
    public boolean isBannedUser(String userID) throws TException {
        UserDAO user_DAO = UserDAO.getInstance(client);
        boolean result = user_DAO.isBannedUser(userID);
        return result;
    }

    @Override
    public String getCounterStatus() throws TException {
        String result = CounterDAO.getCounterStatusString(client);
        return result;
    }

    @Override
    public String getCounterTag() throws TException {
        String result = CounterDAO.getCounterTagString(client);
        return result;
    }

    @Override
    public String listPaging(String keyword) throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        String result = sta_DAO.listPaging(keyword);
        return result;      
    }

    @Override
    public String listStatus(String startKey, String limit) throws TException {
        StatusDAO sta_DAO = StatusDAO.getInstance(client);
        String result = sta_DAO.listStatus(startKey, limit);
        return result;      
    }

    @Override
    public String searchTags(String value, String _start, String _end, String filter) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        String result = "";
        try {
            result = tag.searchTags(value, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public String searchStatusByTag(String value, String _start, String _end, String filter) throws TException {
        StatusDAO sta = StatusDAO.getInstance(client);
        String result ="";
        try {
            result = sta.searchStatusByTag(value, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public String searchStatusByContent(String value, String _start, String _end, String filter) throws TException {
        StatusDAO sta = StatusDAO.getInstance(client);
        String result ="";
        try {
            result = sta.searchStatusByContent(value, _start, _end, filter);
        } catch (Exception ex) {
            Logger.getLogger(AdminImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public String selectStatusFromID(String statusID) throws TException {
        StatusDAO sta = StatusDAO.getInstance(client);
        String result = sta.selectStatusFromID(statusID);
        return result;
    }
        
}
